package com.example.weatherdemotest.util;

import android.net.Uri;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetUtil {

    //以下3行为天气API url相关变量
    private static String BASE_URL = "https://www.yiketianqi.com/free/week";
    private static String APP_ID = "85667527";
    private static String APP_SECRET = "E5kXVVSM";


    /**
     * 使用Get请求
     *
     * @param urlStr
     * @return
     */
    public static String doGet(String urlStr) {
        String result = "";
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;

        //1、连接网络
        HttpURLConnection httpURLConnection = null;
        try {
            URL url = new URL(urlStr);
            //得到Connection
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(5000); //设置超时时间

            //2、从连接中获取数据(二进制)
            InputStream inputStream = httpURLConnection.getInputStream();
            //下面将二进制转为汉字
            inputStreamReader = new InputStreamReader(inputStream);

            //2.1 为所有要处理的数据设置一个缓冲区
            bufferedReader = new BufferedReader(inputStreamReader);

            //2.2 每次从缓冲区中读取1行内容
            //借助StringBulider将读取的line进行拼接(+有同样效果，但效率较低)
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            //2.3 最终的结果存储在StringBuilder中，我们将其赋给result
            result = stringBuilder.toString();

        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            //3、在finally中将使用到的资源关闭
            //3.1 关闭httpURLConnection
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }

            //3.2 关闭inputStreamReader
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            //3.3 关闭bufferedReader
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return result;
    }

    /**
     * 拼接出获取天气的url
     * 请求示例：https://www.yiketianqi.com/free/week?unescape=1&appid=&appsecret=
     * <p>
     * BASE_URL = "https://www.yiketianqi.com/free/week"
     * APP_ID = "85667527"
     * APP_SECRET = "E5kXVVSM";
     *
     * @param city
     * @return
     */
    public static String getWeatherOfCity(String city) {

        String weatherUrl = BASE_URL + "?unescape=1" + "&appid=" + APP_ID + "&appsecret=" + APP_SECRET + "&city=" + city;
        Log.d("SUN", "---WEATHER_URL-------weatherUrl---" + weatherUrl);

        String weatherResult = doGet(weatherUrl);
        Log.d("SUN", "---WEATHER_RESULT------weatherResult---" + weatherResult);

        return weatherResult;
    }
}
